package com.dynamixsoftware.printershare.jbig;

import java.io.IOException;
import java.io.OutputStream;

public final class Jbig85
{
  public static final int JBG_LRLTWO = 64;
  public static final int JBG_VLENGTH = 32;
  static final int MARKER_ABORT = 4;
  static final int MARKER_ATMOVE = 6;
  static final int MARKER_COMMENT = 7;
  static final int MARKER_ESC = 255;
  static final int MARKER_NEWLEN = 5;
  static final int MARKER_RESERVE = 1;
  static final int MARKER_SDNORM = 2;
  static final int MARKER_SDRST = 3;
  static int MARKER_STUFF;
  static final int[] lsztab = { 23069, 9606, 4372, 2059, 984, 474, 229, 111, 54, 26, 13, 6, 3, 1, 23167, 16165, 11506, 8316, 6073, 4482, 3311, 2465, 1839, 1372, 1030, 771, 576, 433, 324, 245, 183, 138, 104, 78, 59, 44, 23265, 18508, 14861, 12017, 9759, 7987, 6568, 5400, 4471, 3700, 3067, 2552, 2145, 1798, 1485, 1246, 1039, 867, 724, 604, 504, 420, 352, 293, 246, 203, 171, 143, 23314, 19716, 16684, 14296, 12264, 10556, 9081, 7903, 6825, 5966, 5156, 4508, 3947, 3409, 2998, 2624, 22578, 19740, 17294, 15325, 13550, 11950, 10650, 9494, 21872, 19625, 17625, 15906, 14372, 12980, 11799, 22184, 20294, 18405, 16847, 15421, 14174, 21041, 19471, 17977, 16734, 22055, 20711, 19333, 21911, 20559, 23056, 21794, 23019 };
  static final int[] nlpstab = { 129, 14, 16, 18, 20, 23, 25, 28, 30, 33, 35, 9, 10, 12, 143, 36, 38, 39, 40, 42, 43, 45, 46, 48, 49, 51, 52, 54, 56, 57, 59, 60, 62, 63, 32, 33, 165, 64, 65, 67, 68, 69, 70, 72, 73, 74, 75, 77, 78, 79, 48, 50, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 61, 193, 80, 81, 82, 83, 84, 86, 87, 87, 72, 72, 74, 74, 75, 77, 77, 208, 88, 89, 90, 91, 92, 93, 86, 216, 95, 96, 97, 99, 99, 93, 223, 101, 102, 103, 104, 99, 105, 106, 107, 103, 233, 108, 109, 110, 111, 238, 112, 240 };
  static final int[] nmpstab = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 9, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 32, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 48, 81, 82, 83, 84, 85, 86, 87, 71, 89, 90, 91, 92, 93, 94, 86, 96, 97, 98, 99, 100, 93, 102, 103, 104, 99, 106, 107, 103, 109, 107, 111, 109, 111 };
  long _i;
  long _y;
  long a;
  int buffer;
  long c;
  int ct;
  long l0;
  int newlen;
  int options;
  OutputStream out;
  long sc;
  byte[] st;
  long x0;
  long y0;
  
  public Jbig85(long paramLong1, long paramLong2, OutputStream paramOutputStream)
  {
    this.x0 = paramLong1;
    this.y0 = paramLong2;
    this.newlen = 0;
    this.out = paramOutputStream;
    this.l0 = 128L;
    this.options = 96;
    this._y = 0L;
    this._i = 0L;
    this.st = new byte[4096];
    ae_reinit();
  }
  final void ae_flush() throws IOException {
      OutputStream var1 = this.out;
      OutputStream localOutputStream = this.out;
      
      long var2 = this.a;
      long var4 = this.c;
      long var6 = this.sc;
      int var8 = this.ct;
      int var9 = this.buffer;
      
      long var10 = 4294901760L & var4 + (var2 - 1L);
      long var12;
      if(var10 < var4) {
         var12 = var10 + 32768L;
      } else {
         var12 = var10;
      }

      long var14 = var12 << var8;
      if((4160749568L & var14) != 0L) {
         if(var9 >= 0) {
            var1.write(var9 + 1);
            if(var9 + 1 == 255) {
               var1.write(0);
            }
         }

         if((134215680L & var14) != 0L) {
            while(var6 != 0L) {
               var1.write(0);
               --var6;
            }
         }
      } else {
         if(var9 >= 0) {
            var1.write(var9);
         }

         while(var6 != 0L) {
            var1.write(255);
            var1.write(0);
            --var6;
         }
      }

      if((134215680L & var14) != 0L) {
         var1.write((int)(255L & var14 >> 19));
         if((255L & var14 >> 19) == 255L) {
            var1.write(0);
         }

         if((522240L & var14) != 0L) {
            var1.write((int)(255L & var14 >> 11));
            if((255L & var14 >> 11) == 255L) {
               var1.write(0);
            }
         }
      }
      this.a = var2;
      this.c = var14;
      this.sc = var6;
      this.ct = var8;
      this.buffer = var9;
   }
  
  final void ae_reinit()
  {
    this.c = 0L;
    this.a = 65536L;
    this.sc = 0L;
    this.ct = 11;
    this.buffer = -1;
  }
  
  public final void enc_lineout(byte[] var1, byte[] var2) throws IOException {
      OutputStream var3 = this.out;
      long var4 = this.x0;
      long var6 = this.y0;
      long var8 = this.l0;
      int var10 = this.options;
      long var11 = this._y;
      long var13 = this._i;
      byte[] var15 = new byte[20];
      
      
      
      if(var11 < var6) {
         if(var11 == 0L) {
            var15[0] = 0;
            var15[1] = 0;
            var15[2] = 1;
            var15[3] = 0;
            var15[4] = (byte)((int)(255L & var4 >> 24));
            var15[5] = (byte)((int)(255L & var4 >> 16));
            var15[6] = (byte)((int)(255L & var4 >> 8));
            var15[7] = (byte)((int)(255L & var4));
            var15[8] = (byte)((int)(255L & var6 >> 24));
            var15[9] = (byte)((int)(255L & var6 >> 16));
            var15[10] = (byte)((int)(255L & var6 >> 8));
            var15[11] = (byte)((int)(255L & var6));
            var15[12] = (byte)((int)(255L & var8 >> 24));
            var15[13] = (byte)((int)(255L & var8 >> 16));
            var15[14] = (byte)((int)(255L & var8 >> 8));
            var15[15] = (byte)((int)(255L & var8));
            var15[16] = 0;
            var15[17] = 0;
            var15[18] = 0;
            var15[19] = (byte)(var10 & 96);
            var3.write(var15, 0, 20);
         }

         if(var13 == 0L) {
            this.output_newlen();
            this.ae_reinit();
         }

         long var16 = var4 >> 3;
         byte var18;
         if((7L & var4) != 0L) {
            var18 = 1;
         } else {
            var18 = 0;
         }

         long var19 = var16 + (long)var18;
         if((7L & var4) != 0L) {
            var1[(int)(var19 - 1L)] = (byte)(255 & var1[(int)(var19 - 1L)] & ~(-1 + (1 << (int)(8L - (7L & var4)))));
         }

         int var21;
         if(var11 < 1L) {
            var21 = -1;
         } else {
            var21 = 0;
         }

         int var22 = 0;
         long var23 = 0L;
         long var25 = var23;
         if(var21 != -1) {
            var23 = (long)(255 & var2[var21]) << 8;
         }

         
         
         byte[] var27 = this.st;
         long var28 = this.a;
         long var30 = this.c;
         long var32 = this.sc;
         int var34 = this.ct;
         int var35 = this.buffer;
         long var36 = 0L;

         while(var36 < var4) {
            var25 |= (long)(255 & var1[var22]);
            if(var36 < 8L * var19 - 8L && var21 != -1) {
               var23 |= (long)(255 & var2[var21 + 1]);
            }

            do {
               label118: {
                  var25 <<= 1;
                  var23 <<= 1;
                  int var42 = (int)(1008L & var23 >> 10 | 15L & var25 >> 9);
                  int var43 = (int)(1L & var25 >> 8);
                  int var44 = 127 & var27[var42];
                  int var45 = lsztab[var44];
                  if((128 & (var43 << 7 ^ var27[var42])) != 0) {
                     var28 -= (long)var45;
                     if(var28 >= (long)var45) {
                        var30 += var28;
                        var28 = (long)var45;
                     }

                     var27[var42] = (byte)(128 & var27[var42] ^ nlpstab[var44]);
                  } else {
                     var28 -= (long)var45;
                     if((4294934528L & var28) != 0L) {
                        break label118;
                     }

                     if(var28 < (long)var45) {
                        var30 += var28;
                        var28 = (long)var45;
                     }

                     var27[var42] = (byte)(128 & var27[var42] | nmpstab[var44]);
                  }

                  do {
                     var28 <<= 1;
                     var30 <<= 1;
                     --var34;
                     if(var34 == 0) {
                        long var46 = var30 >> 19;
                        if((4294967040L & var46) != 0L) {
                           if(var35 >= 0) {
                              int var48 = var35 + 1;
                              var3.write(var48);
                              if(var48 == 255) {
                                 var3.write(0);
                              }
                           }

                           while(var32 != 0L) {
                              var3.write(0);
                              --var32;
                           }

                           var35 = (int)(255L & var46);
                        } else if(var46 == 255L) {
                           ++var32;
                        } else {
                           if(var35 >= 0) {
                              var3.write(var35);
                           }

                           while(var32 != 0L) {
                              var3.write(255);
                              var3.write(0);
                              --var32;
                           }

                           var35 = (int)(255L & var46);
                        }

                        var30 &= 524287L;
                        var34 = 8;
                     }
                  } while(var28 < 32768L);
               }

               ++var36;
            } while((7L & var36) != 0L && var36 < var4);

            ++var22;
            if(var21 != -1) {
               ++var21;
            }
         }
         

         this.a = var28;
         this.c = var30;
         this.sc = var32;
         this.ct = var34;
         this.buffer = var35;
         long var38 = var13 + 1L;
         long var40 = var11 + 1L;
         if(var38 == var8 || var40 == var6) {
            this.ae_flush();
            var15[0] = -1;
            var15[1] = 2;
            var3.write(var15, 0, 2);
            var38 = 0L;
            this.output_newlen();
         }
         this._y = var40;
         this._i = var38;
      }
   }

  public final void enc_newlen(long var1) throws IOException {
      byte[] var3 = new byte[6];
      if(this.newlen != 2 && var1 < this.y0 && var1 >= 1L && (32 & this.options) != 0) {
         if(var1 < this._y) {
            var1 = this._y;
         }

         if(this._y > 0L && this.y0 != var1) {
            this.newlen = 1;
         }

         this.y0 = var1;
         if(this._y == this.y0) {
            if(this._i > 0L) {
               this.ae_flush();
               var3[0] = -1;
               var3[1] = 2;
               this.out.write(var3, 0, 2);
               this._i = 0L;
            }

            this.output_newlen();
            return;
         }
      }

   }
  
  final void output_newlen() throws IOException {
      byte[] var1 = new byte[6];
      if(this.newlen == 1) {
         var1[0] = -1;
         var1[1] = 5;
         var1[2] = (byte)((int)(255L & this.y0 >> 24));
         var1[3] = (byte)((int)(255L & this.y0 >> 16));
         var1[4] = (byte)((int)(255L & this.y0 >> 8));
         var1[5] = (byte)((int)(255L & this.y0));
         this.out.write(var1, 0, 6);
         this.newlen = 2;
         if(this._y == this.y0) {
            var1[1] = 2;
            this.out.write(var1, 0, 2);
            return;
         }
      }

   }  
}
